package com.fengze.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.fengze.business.domain.PerformancePlan;
import com.fengze.business.mapper.PerformancePlanMapper;
import com.fengze.business.vo.PerformanceRecordVO;
import com.fengze.common.annotation.AutoSetInsertValue;
import com.fengze.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fengze.business.mapper.PerformanceRecordMapper;
import com.fengze.business.domain.PerformanceRecord;
import com.fengze.business.service.IPerformanceRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 履约记录Service业务层处理
 * 
 * @author lug
 * @date 2024-02-04
 */
@Service
public class PerformanceRecordServiceImpl implements IPerformanceRecordService 
{
    @Autowired
    private PerformanceRecordMapper performanceRecordMapper;

    @Autowired
    private PerformancePlanMapper performancePlanMapper;

    /**
     * 查询履约记录
     * 
     * @param id 履约记录主键
     * @return 履约记录
     */
    @Override
    public PerformanceRecord selectPerformanceRecordById(Long id)
    {
        return performanceRecordMapper.selectPerformanceRecordById(id);
    }

    /**
     * 查询履约记录列表
     * 
     * @param performanceRecord 履约记录
     * @return 履约记录
     */
    @Override
    public List<PerformanceRecord> selectPerformanceRecordList(PerformanceRecord performanceRecord)
    {
        return performanceRecordMapper.selectPerformanceRecordList(performanceRecord);
    }

    /**
     * 新增履约记录
     * 
     * @param performanceRecord 履约记录
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @AutoSetInsertValue
    @Override
    public int insertPerformanceRecord(PerformanceRecord performanceRecord)
    {

        // 查询合同id
        PerformancePlan performancePlan = performancePlanMapper.selectPerformancePlanById(performanceRecord.getPerformancePlanId());
        performanceRecord.setContractId(performancePlan.getContractId());

        // 累加履行金额
        performancePlanMapper.plusMoney(performancePlan.getId(),performanceRecord.getCollectionMoney());
        return performanceRecordMapper.insertPerformanceRecord(performanceRecord);
    }

    /**
     * 修改履约记录
     * 
     * @param performanceRecord 履约记录
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updatePerformanceRecord(PerformanceRecord performanceRecord)
    {

        // 更新履行额度
        PerformanceRecord dbObj = performanceRecordMapper.selectPerformanceRecordById(performanceRecord.getId());
        BigDecimal old = dbObj.getCollectionMoney();
        BigDecimal now = performanceRecord.getCollectionMoney();

        int result = old.compareTo(now);
        // 相等
        if(result == 0){

        }else if( result == 1){
            // 大于
            BigDecimal subtract = old.subtract(now);
            performancePlanMapper.subtractById(performanceRecord.getPerformancePlanId(),subtract);
        }else if( result == -1 ){
            // 小于
            BigDecimal subtract = now.subtract(old);
            performancePlanMapper.plusMoney(performanceRecord.getPerformancePlanId(),subtract);
        }


        return performanceRecordMapper.updatePerformanceRecord(performanceRecord);
    }

    public static void main(String[] args) {
        BigDecimal one = new BigDecimal(10);
        BigDecimal two = new BigDecimal(10);
        BigDecimal three = new BigDecimal(12.3);

        System.out.println(one.compareTo(two));
        System.out.println(one.compareTo(three));
        System.out.println(three.compareTo(one));

        System.out.println(one.subtract(three));
    }

    /**
     * 批量删除履约记录
     * 
     * @param ids 需要删除的履约记录主键
     * @return 结果
     */

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deletePerformanceRecordByIds(Long[] ids)
    {
        for (Long id : ids) {
            PerformanceRecord record = performanceRecordMapper.selectPerformanceRecordById(id);
            performancePlanMapper.subtractById(record.getPerformancePlanId(),record.getCollectionMoney());
            
        }
        return performanceRecordMapper.deletePerformanceRecordByIds(ids);
    }

    /**
     * 删除履约记录信息
     * 
     * @param id 履约记录主键
     * @return 结果
     */
    @Override
    public int deletePerformanceRecordById(Long id)
    {
        return performanceRecordMapper.deletePerformanceRecordById(id);
    }

    @Override
    public PerformanceRecordVO selectVOById(Long id) {
        return performanceRecordMapper.selectVOById(id);
    }

    @Override
    public List<PerformanceRecordVO> selectPerformanceRecordVOList(PerformanceRecord performanceRecord) {
        return performanceRecordMapper.selectPerformanceRecordVOList(performanceRecord);
    }
}
